"""
2020年全国疫情地图
"""
import json

from pyecharts.charts import Map
from pyecharts.options import *
import os

# 读取文件
f = open('D:\Document\python学习\资料\可视化案例数据\地图数据\疫情.txt', 'r', encoding='UTF-8')
f_data = f.read()
f.close()

# JSON 格式转为 dict
dict_data = json.loads(f_data)

# 取到各省数据
province_data_list = dict_data['areaTree'][0]['children']

# 创建列表，并将各省数据组装成元组存入列表
data_list = []
for province in province_data_list:
    province_data = (province['name'], province['total']['confirm'])
    data_list.append(province_data)

# 创建地图对象
yq_map = Map()

# 添加数据
yq_map.add('2020年全国各省份确诊人数', data_list, 'china')

# 设置全局选项
yq_map.set_global_opts(
    title_opts=TitleOpts('全国疫情地图'),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min": 1, "max": 99, "label": "1-99", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "label": "100-999", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "label": "1000-4999", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "label": "50000-9999", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "label": "10000-99999", "color": "#CC3333"},
            {"min": 100000, "label": "100000+", "color": "#990033"},
        ]
    )
)

# 生成地图
yq_map.render()

# 打开地图
os.system('render.html')
